﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

namespace UserInterface
{
    using BusinessLogicLayer;
    using BusinessObjectLayer;
    using ComplexDataType;
    using JntMsgBox;
    using System.Net;
    using System.IO;
    using BusinessLogicLibrary;
    using System.Drawing.Drawing2D;

    public partial class EditGalleryUI : Form
    {    
        private ResourceManager _ResourceManager = new ResourceManager("UserInterface.Properties.Resources", Assembly.GetExecutingAssembly());
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        string UpdateText;
        string DeleteText;
        string UpdateConfirmation;
        string DeleteConfirmation;

        public EditGalleryUI()
        {
            InitializeComponent();
            uploadButton.Text = _ResourceManager.GetString("CreateButtonText");
            this.Text = _ResourceManager.GetString("AddNewGallery");
        }
        private void SetResourceManager()
        {
            try
            {
                UpdateText = _ResourceManager.GetString("Update");
                DeleteText = _ResourceManager.GetString("Delete");
                UpdateConfirmation = _ResourceManager.GetString("UpdateConfirmation");
                DeleteConfirmation = _ResourceManager.GetString("DeleteConfirmation");
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            CloseForm();
        }
        private void CloseForm()
        {
            this.Close();
        }
        private void editButton_Click(object sender, EventArgs e)
        {
            if (ValidateItem())
                GetEditConfirmtion();
        }
        private bool ValidateItem()
        {
            bool isValid = true;
            if (imageURLTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(imageURLTextBox, "*");
                isValid = false;
            }         
            return isValid;
        }

        private string FileName(string path)
        {
            return path.Substring(path.LastIndexOf("\\")).Replace("\\", "");
        }
        private void GetEditConfirmtion()
        {
            try
            {
                Insert();
                CloseForm();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void Insert()
        {
            try
            {           
                string imageName = openImageDialog.FileName.Substring(openImageDialog.FileName.LastIndexOf("\\")).Replace("\\", "");
                string imageDestination = string.Format("{0}/Image/{1}", ServerBLL.GetStringValue("GalleryPath"), imageName);
                string thumbnailName = openImageDialog.FileName.Substring(openImageDialog.FileName.LastIndexOf("\\")).Replace("\\", "");
                string thumbnailDestination = string.Format("{0}/Thumbnails/{1}", ServerBLL.GetStringValue("GalleryPath"), thumbnailName);
                FileBLL _FileBLL = new FileBLL();              
                Image image = Helper.ResizeImage(openImageDialog.FileName, 160,120, false);
                string thumbnailTemp = string.Format("{0}/Image{1}",ServerBLL.GetStringValue("TempPath"),DateTime.Now.Millisecond);
                image.Save(thumbnailTemp);
                _FileBLL.Write(new FileBOL(openImageDialog.FileName, imageDestination, false));
                _FileBLL.Write(new FileBOL(thumbnailTemp, thumbnailDestination, false));   
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }       
        private void noticeLabel_Click(object sender, EventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
            _ExceptionHandlerBOL = null;
        }       
        private void imageURLButton_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = openImageDialog.ShowDialog();
                if (dialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    imageURLTextBox.Text = openImageDialog.FileName;
                    galleryPictureBox.Image = Image.FromFile(imageURLTextBox.Text);
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }    
    }
}